package net.crowdloud.shared.engine;

import java.util.ArrayList;
import java.util.List;

import net.crowdloud.shared.entities.Problem;
import net.crowdloud.shared.entities.Solution;
import net.crowdloud.shared.entities.User;

public class CrowdLoudCustomerEngine {

	public static List<User> getUsersCustomers(List<User> allUsers,
			User userProvider) {

		List<User> usersCustomers = new ArrayList<User>();

		List<String> solutionTags = new ArrayList<String>();

		for (Solution solution : userProvider.getUserSolutions()) {

			if (solution.getTags() != null) {

				for (String solutionTag : solution.getTags().split(",")) {

					solutionTags.add(solutionTag);
				}
			}
		}

		for (User user : allUsers) {

			boolean foundCustomer = false;

			for (Problem customerProblem : user.getUserProblems()) {

				if (customerProblem.getTags() != null) {

					for (String customerProblemTag : customerProblem.getTags()
							.split(",")) {

						for (String solutionTag : solutionTags) {

							if (solutionTag != null && solutionTag.length() > 0
									&& solutionTag.equals(customerProblemTag)) {

								usersCustomers.add(user);

								foundCustomer = true;

								break;
							}
						}

						if (foundCustomer) {

							break;
						}
					}

					if (foundCustomer) {

						break;
					}
				}
			}
		}

		return usersCustomers;
	}

}
